package com.qianfeng.shopping.dao;

import java.io.*;
import java.util.*;

import com.qianfeng.shopping.entity.Account;
import com.qianfeng.shopping.entity.Level;
import com.qianfeng.shopping.entity.Role;
import com.qianfeng.shopping.util.GetUuid;
import com.qianfeng.shopping.util.ReadWriteTxt;

// 持久层  处理数据
public class AccountDaoImp implements AccountDao {
	//  创建集合存储元素
	static List<Account> list = new ArrayList<>();
	static Map<String,Account> map = new HashMap<>();
	static File file = new File("src/com/qianfeng/shopping/dao/accountinfo.txt");
	// 登录
	@Override
	public Account login(Account account) {
		map.clear();
		// 读取数据
		map = selectAll();
		Collection<Account> c = map.values();
		for (Account acc:c){
			if (acc!=null&&acc.getAccountName().equals(account.getAccountName())
					&&acc.getAccountPwd().equals(account.getAccountPwd())){
				return acc;
			}
		}
		return null;
	}
	// 注册
	@Override
	public boolean register(Account account) {
		map.clear();
		// 获取元素个数 为了提供账户id 效率低
		map = selectAll();
		// 默认注册买家
		Role role2 = new Role(2,"买家","拥有购买权限");
		account.setRole(role2);
		//  可能会因为删除导致id重复 可以利用uuid
		String id = "00"+(map.size()+1);
		account.setAccountId(id);
		// 默认注册一个白银会员
		Level level = new Level();
		level.setLevelid(1);
		level.setLevelname("白银会员");
		level.setDiscount(0.95);
		account.setLevel(level);
		// 写入字符串
		String str = account.getAccountId()+"#"+account.getAccountName()+"#"+account.getAccountPwd()
				+"#"+account.getAccountRealName()+"#"+account.getAccountPhone()+"#"+account.getAccountBirthday()
				+"#"+account.getRole().getRoleId()+"#"+account.getRole().getRoleName()+"#"+account.getRole().getRoleMemo()
				+"#"+account.getLevel().getLevelid()+"#"+account.getLevel().getLevelname()+"#"+account.getLevel().getDiscount();
		Map<String,String> mapWrite= new HashMap<>();
		mapWrite.put(GetUuid.getUuid(),str);
		return ReadWriteTxt.writeTxt(file,mapWrite,true);
	}
	// 检查用户名是否存在
	@Override
	public boolean checkAccountName(String accountName) {
		for(Account acc:map.values()){
			if(acc.getAccountName().equals(accountName)){
				return false;
			}
		}
		return true;
	}
	// 一览信息
	@Override
	public Map<String,Account> selectAll() {
		Map<String,String> mapRead = ReadWriteTxt.readTxt(file);
		Set<Map.Entry<String,String>> set =mapRead.entrySet();
		Iterator<Map.Entry<String,String>> it = set.iterator();

		Map<String,Account> map = new HashMap<>();
		while(it.hasNext()) {
			Map.Entry<String, String> entry = it.next();
			String str = (String) (entry.getValue());
			Account account = new Account();
			// 拆分字符串  得到的是该对象的属性
			String[] arr = str.split("#");
			//  account的属性和拆分的列对应
			account.setAccountId(arr[0]);
			account.setAccountName(arr[1]);
			account.setAccountPwd(arr[2]);
			account.setAccountRealName(arr[3]);
			account.setAccountPhone(arr[4]);
			account.setAccountBirthday(arr[5]);
			Role role = new Role();
			// String转换成int类型
			role.setRoleId(Integer.parseInt(arr[6]));
			role.setRoleName(arr[7]);
			role.setRoleMemo(arr[8]);
			Level level = new Level();
			level.setLevelid(Integer.parseInt(arr[9]));
			level.setLevelname(arr[10]);
			level.setDiscount(Double.parseDouble(arr[11]));
			//  赋值到account中
			account.setRole(role);
			account.setLevel(level);
			map.put(GetUuid.getUuid(), account);
		}
		return map;
	}
	// 查询
	@Override
	public Map<String,Account>select(String key) {
		return map;
	}
	// 查询id是否存在
	@Override
	public boolean checkAccountId(String id) {
		// 先清除以前的数据
		map.clear();
		// 读取最新的文本内容
		Map<String,Account> map = this.selectAll();
		for(Account acc:map.values()){
			if(acc.getAccountId().equals(id)){
				return true;
			}
		}
		return false;
	}
	// 删除  读取String 写入也是String
	public boolean delete2(String id){
		// 利用共通方法 读取txt内容 收集到Map集合
		Map<String,String> mapRead = ReadWriteTxt.readTxt(file);
		Set<Map.Entry<String,String>> set =mapRead.entrySet();
		Iterator<Map.Entry<String,String>> it = set.iterator();
		while(it.hasNext()){
			Map.Entry<String,String> entry = it.next();
			String str = (String)(entry.getValue());
			if (str.split("#")[0].equals(id)){
				mapRead.remove(entry.getKey());
				break;
			}
		}
		//  写入
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
			// 覆盖
			fw = new FileWriter(file,false);
			bw = new BufferedWriter(fw);
			Set<Map.Entry<String,String>> setNew = mapRead.entrySet();
			Iterator<Map.Entry<String,String>> itNew = setNew.iterator();
			// 最后一次循环写完之后不换行
			while(itNew.hasNext()) {
				Map.Entry<String, String> entry = itNew.next();
				String str = (String) (entry.getValue());
				bw.write(str);
				// 判断是否有下一个元素  到最后一个元素 结果false  不用换行
				if(itNew.hasNext()){
					bw.newLine();
				}
			}
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				bw.flush();
				bw.close();
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return  false;
	}


	// 删除
	@Override
	public boolean delete(String id) {
		map.clear();
		map = this.selectAll();
		// 创建要写入的map
		Map<String,String> mapWrite = new HashMap<>();
		// 通过账户id移除数据
		Set<Map.Entry<String,Account>> set = map.entrySet();
		Iterator<Map.Entry<String,Account>> it = set.iterator();
		while(it.hasNext()){
			Map.Entry<String,Account> entry = it.next();
			Account account = (Account)(entry.getValue());
			String str = account.getAccountId() + "#" + account.getAccountName() + "#" + account.getAccountPwd()
					+ "#" + account.getAccountRealName() + "#" + account.getAccountPhone() + "#" + account.getAccountBirthday()
					+ "#" + account.getRole().getRoleId() + "#" + account.getRole().getRoleName() + "#" + account.getRole().getRoleMemo()
					+ "#" + account.getLevel().getLevelid() + "#" + account.getLevel().getLevelname() + "#" + account.getLevel().getDiscount();
			if (account.getAccountId().equals(id)){
				continue;
			}else{
				// 如果删除的id相等 则不存放到写入的map中
				// 如果不是删除的id数据  收集到map中
				mapWrite.put(GetUuid.getUuid(),str);
			}
		}
		return ReadWriteTxt.writeTxt(file,mapWrite,false);
	}
	// 修改
	@Override
	public boolean update(Account acc) {
		map.clear();
		map = this.selectAll();
		// 创建要写入的map
		Map<String,String> mapWrite = new HashMap<>();
		// 通过账户id移除数据
		Set<Map.Entry<String,Account>> set = map.entrySet();
		Iterator<Map.Entry<String,Account>> it = set.iterator();
		while(it.hasNext()){
			Map.Entry<String,Account> entry = it.next();
			Account account = (Account)(entry.getValue());
			// 找到修改的数据  然后覆盖属性
			if (account.getAccountId().equals(acc.getAccountId())){
				account.setAccountRealName(acc.getAccountRealName());
				account.setAccountBirthday(acc.getAccountBirthday());
				account.setAccountPwd(acc.getAccountPwd());
				account.setAccountPhone(acc.getAccountPhone());
			}
			// 拼接字符串
			String str = account.getAccountId() + "#" + account.getAccountName() + "#" + account.getAccountPwd()
					+ "#" + account.getAccountRealName() + "#" + account.getAccountPhone() + "#" + account.getAccountBirthday()
					+ "#" + account.getRole().getRoleId() + "#" + account.getRole().getRoleName() + "#" + account.getRole().getRoleMemo()
					+ "#" + account.getLevel().getLevelid() + "#" + account.getLevel().getLevelname() + "#" + account.getLevel().getDiscount();
			// 任何数据都要存储到map
			mapWrite.put(GetUuid.getUuid(),str);
		}
		return ReadWriteTxt.writeTxt(file,mapWrite,false);
	}
	public static void main(String[] args) {
		// 主程序入口  测试当前类
		File file = new File("E:\\code\\shopping2205_array\\src\\com\\qianfeng\\shopping\\dao\\accountinfo.txt");
		AccountDaoImp accountDaoImp = new AccountDaoImp();
//		System.out.println(accountDaoImp.delete("006"));
//		System.out.println(accountDaoImp.selectAll());
//		Account[] arr = accountDaoImp.select("e");
//		System.out.println(arr.toString());
//		System.out.println(Arrays.toString(arr));
//		for (int i = 0; i < arr.length; i++) {
//			System.out.println(arr[i].toString());
//		}
		Account account = new Account();
		account.setAccountId("005");
		account.setAccountName("li");
		account.setAccountPwd("999");
		account.setAccountPhone("999999999");
		account.setAccountBirthday("2023年2月6日");
		account.setAccountRealName("李逵");
		accountDaoImp.update(account);
//		System.out.println(accountDaoImp.register(account));
//		System.out.println(accountDaoImp.login(account));
//		System.out.println(accountDao.checkAccountName("admin123"));
		
//		String[] array = {"002","003","123"};
//		System.out.println(accountDaoImp.deleteBatch(array));
//		System.out.println(Arrays.toString(AccountDaoImp.arr));
	}
	@Override
	public int deleteBatch(String[] array) {
		int count = 0;
//		// 重写批量删除方法
//		for (int i = 0; i < arr.length; i++) {
//			for (int j = 0; j < array.length; j++) {
//				if (arr[i]!=null&&arr[i].getAccountId().equals(array[j])) {
//					arr[i] = null;
//					count++;
//					break;
//				}
//			}
//		}
		return count;
	}

	@Override
	public Account selectAccount(String id) {
		return null;
	}
}
